<?php

namespace app\job\queue;

use app\common\enum\fp\PostAuditEnum;
use app\logic\es\fp\EsPost;
use think\facade\Log;
use think\queue\Job;
use app\common\library\aliyuns\MtsClient;
use app\common\model\fp\PostMedia;
use think\Exception;

/**
 * 视频转码
 */
class MpsVideoTransferJob
{
    public function fire(Job $job, $data)
    {
        if ($job->attempts() > 10) {
            //通过这个方法可以检查这个任务已经重试了几次了
            $job->failed($data);
            $job->delete();
            return;
        }
        try {
            if (PostAuditEnum::from($data['status']) === PostAuditEnum::APPROVED) {

                $postMedia = PostMedia::initModel($data['user_id'])->where('post_id', $data['post_id'])->select()->toArray();

                //视频处理
                if (isset($postMedia[0]) && $postMedia[0]['media_type'] == 'video') {

                    //TODO 视频转码
                }

                //如果任务执行成功后 记得删除任务，不然这个任务会重复执行，直到达到最大重试次数后失败后，执行failed方法


                //导入动态到es
                EsPost::getInstance()->import($data);

                $job->delete();
            }
        } catch (\Throwable $e) {
            Log::channel('job')->error($e->getMessage() . PHP_EOL . $e->getTraceAsString());
            throw $e;
        }
    }
}